home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / net / bind-contrib.tar.gz / bind-contrib.tar / contrib / arlib / sample.c < prev    next >
C/C++ Source or Header  |  1996-10-25  |  3KB  |  143 lines

  1. #include <stdio.h>
  2. #include <strings.h>
  3. #include <errno.h>
  4. #include <sys/types.h>
  5. #include <sys/time.h>
  6. #include <netinet/in.h>
  7. #include <netdb.h>
  8. #include "arlib.h"
  9.  
  10. #ifndef    lint
  11. static    char    sccsid[] = "@(#)sample.c    1.1 12/21/92 (C)1992 Darren Reed. ASYNC DNS";
  12. #endif
  13.  
  14. char    line[512];
  15.  
  16. int    lookup = 0, seq = 0;
  17. long    expire = 0;
  18.  
  19. main()
  20. {
  21.     struct    in_addr adr;
  22.     struct    timeval    tv2;
  23.     fd_set    rd;
  24.     long    now;
  25.     char    *s;
  26.     int    afd, nfd, pid = getpid(), del;
  27.  
  28.     afd = ar_init(ARES_INITLIST|ARES_CALLINIT|ARES_INITSOCK);
  29.  
  30.     (void)printf("afd = %d pid = %d\n",afd, pid);
  31.  
  32.     while (1)
  33.     {
  34.         (void)printf("Host =>");
  35.         (void)fflush(stdout);
  36.         *line = '\0';
  37.         FD_ZERO(&rd);
  38.         FD_SET(0,&rd);
  39.         FD_SET(afd,&rd);
  40.         now = time(NULL);
  41.         if (expire >= now)
  42.             {
  43.             tv2.tv_usec = 0;
  44.             tv2.tv_sec = expire - now;
  45.             nfd = select(FD_SETSIZE, &rd, NULL, NULL, &tv2);
  46.             }
  47.         else
  48.             nfd = select(FD_SETSIZE, &rd, NULL, NULL, NULL);
  49.  
  50.         if (FD_ISSET(0, &rd))
  51.         {
  52.             if (!fgets(line, sizeof(line) - 1, stdin))
  53.                 exit(0);
  54.             if (s = index(line, '\n'))
  55.                 *s = '\0';
  56.         }
  57.  
  58.         if (isalpha(*line))
  59.         {
  60.             (void)printf("Asking about [%s] #%d.\n",line, ++seq);
  61.             (void)ar_gethostbyname(line, (char *)&seq, sizeof(seq));
  62.             lookup++;
  63.         }
  64.         else if (isdigit(*line))
  65.         {
  66.             (void)printf("Asking about IP#[%s] #%d.\n",
  67.                 line, ++seq);
  68.             adr.s_addr = inet_addr(line);
  69.             (void)ar_gethostbyaddr((char *)&adr, (char *)&seq,
  70.                         sizeof(seq));
  71.             lookup++;
  72.         }
  73.         if (lookup)
  74.             (void)printf("Waiting for answer:\n");
  75.         if (FD_ISSET(afd, &rd))
  76.             (void)waitonlookup(afd);
  77.         del = 0;
  78.         expire = ar_timeout(time(NULL), &del, sizeof(del));
  79.         if (del)
  80.         {
  81.             (void)fprintf(stderr,"#%d failed\n", del);
  82.             lookup--;
  83.         }
  84.     }
  85. }
  86.  
  87. printhostent(hp)
  88. struct hostent *hp;
  89. {
  90.     struct in_addr ip;
  91.     int i;
  92.  
  93.     (void)printf("hname = %s\n", hp->h_name);
  94.     for (i = 0; hp->h_aliases[i]; i++)
  95.         (void)printf("alias %d = %s\n", i+1, hp->h_aliases[i]);
  96.     for (i = 0; hp->h_addr_list[i]; i++)
  97.     {
  98.         bcopy(hp->h_addr_list[i], (char *)&ip, sizeof(ip));
  99.         (void)printf("IP# %d = %s\n", i+1, inet_ntoa(ip));
  100.     }
  101. }
  102.  
  103. int    waitonlookup(afd)
  104. int    afd;
  105. {
  106.     struct    timeval delay;
  107.     struct    hostent    *hp;
  108.     fd_set    rd;
  109.     long    now;
  110.     int    nfd, del;
  111.  
  112. waitloop:
  113.     FD_ZERO(&rd);
  114.     now = time(NULL);
  115.     if (expire >= now)
  116.         delay.tv_sec = expire - now;
  117.     else
  118.         delay.tv_sec = 1;
  119.     delay.tv_usec = 0;
  120.     FD_SET(afd, &rd);
  121.     FD_SET(0, &rd);
  122.  
  123.     nfd = select(FD_SETSIZE, &rd, 0, 0, &delay);
  124.     if (nfd == 0)
  125.         return 0;
  126.     else if (FD_ISSET(afd, &rd))
  127.     {
  128.         del = 0;
  129.         hp = ar_answer(&del, sizeof(del));
  130.  
  131.         (void)printf("hp=%x seq=%d\n",hp,del);
  132.         if (hp)
  133.             {
  134.             (void)printhostent(hp);
  135.             if (!--lookup)
  136.                 return 1;
  137.             }
  138.     }
  139.     if (FD_ISSET(0, &rd))
  140.         return 2;
  141.     return 0;
  142. }
  143.